﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Diagnostics;
using System.Collections;
using System.Drawing;
using System.Windows.Forms;

namespace FCNS.KeepMoneyLibrary.Data
{
    public class GroupField
    {
        private static DataRow[] GetAllSubGroupRows(int parentGroupId)
        {
            List<DataRow> rows = new List<DataRow>();
            DataRow[] r = KMData.DataTableGroups.Select("ParentGroupId = " + parentGroupId);
            rows.AddRange(r);
            foreach (DataRow row in r)
                rows.AddRange(GetAllSubGroupRows((int)row["Id"]));

            return rows.ToArray();
        }

        /// <summary>
        /// 更改此群组及子群组的所属父群组
        /// </summary>
        /// <param name="newGroupId"></param>
        public void ChangeParentGroup(int newGroupId)
        {
            foreach (GroupField field in fields.FindAll(gf => { return gf.ParentGroupId == Id; }))
                field.ParentGroupId = ParentGroupId;

            ParentGroupId = newGroupId;
        }

        /// <summary>
        /// 查找所有低于此群组的节点是有存在指定节点
        /// </summary>
        /// <param name="subGroupId"></param>
        /// <returns></returns>
        public bool SearchAllSubField(int subGroupId)
        {
            return SearchField(Id, subGroupId);
        }
        private bool SearchField(int parentId, int subId)
        {
            bool b = false;
            foreach (GroupField field in fields.FindAll(gf => { return gf.ParentGroupId == parentId; }))
            {
                if (field.Id == subId)
                    b = true;

                if (b)
                    break;
                if (!b)
                    b = SearchField(field.Id, subId);
            }

            return b;
        }

        public static bool GetGroupType(int groupId)
        {
            Debug.Assert(fields.Count != 0);

            return fields.Find(f => { return f.Id == groupId; }).IorE;
        }

        public static List<int> GetAllSubGroupIds(int parentGroupId)
        {
            List<int> ids = new List<int>();
            foreach (DataRow row in GetAllSubGroupRows(parentGroupId))
                ids.Add((int)row["Id"]);

            return ids;
        }

        public static string[] GetGroupNames(List<GroupField> fields)
        {
            List<string> comboxItems = new List<string>();
            foreach (GroupField gf in fields)
            {
                if (gf.ParentGroupId == 0)
                {
                    gf.index = comboxItems.Count;
                    comboxItems.Add(gf.Name);
                    if (fields.Find(f => { return f.ParentGroupId == gf.Id; }) != null)
                        ReFillGroups(1, gf.Id, fields, comboxItems);
                }
            }
            return comboxItems.ToArray();
        }

        private static void ReFillGroups(int level, int Id, List<GroupField> fields, List<string> comboxItems)
        {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < level; i++)
                sb.Append("..");

            foreach (GroupField gf in fields.FindAll(delegate(GroupField gfield) { return gfield.ParentGroupId == Id; }))
            {
                gf.index = comboxItems.Count;
                comboxItems.Add(sb.ToString() + gf.Name);
                if (fields.Find(delegate(GroupField gfield) { return gfield.ParentGroupId == gf.Id; }) != null)
                    ReFillGroups(level + 1, gf.Id, fields, comboxItems);

            }
            sb.Remove(0, sb.Length);
        }

        public static GroupField GetDefaultGroup(bool income)
        {
            DataRow o = KMData.DataTableGroups.Select("Id <> -1 and ParentGroupId = 0 and IorE = " + income.ToString())[0];
            if (o == null)
                o = KMData.DataTableGroups.Select("Id <> -1 and IorE = " + income.ToString())[0];

            if (o == null)
            {
                Debug.Assert(false);
                return null;
            }
            else
                return new GroupField(o);
        }


        static List<GroupField> fields = new List<GroupField>();
        public static List<GroupField> GetGroupField
        {
            get
            {
                if (fields.Count == 0)
                {
                    foreach (DataRow row in KMData.DataTableGroups.Rows)
                        fields.Add(new GroupField(row));

                    Debug.Assert(fields.Count != 0, "GroupField");
                }
                return fields;
            }
        }

        public void Remove(bool removeEntryField)
        {
            foreach (GroupField group in fields.FindAll(gf => { return gf.ParentGroupId == Id; }))
                group.Remove(removeEntryField);

            if (removeEntryField)
                foreach (EntryField f in EntryField.GetEntryField.FindAll(ef => { return ef.GroupId == Id; }))
                    f.Remove();

            KMData.DataTableGroups.Rows.Remove(dRow);
            fields.Remove(this);
        }

        DataRow dRow = null;
        public DataRow Row
        {
            get { return dRow; }
        }

        public GroupField(DataRow row)
        {
            dRow = row;
        }

        bool add = false;
        public void AddNewRowToDataTable()
        {
            if (!add)
                return;

            KMData.DataTableGroups.Rows.Add(dRow);
            fields.Add(this);
            add = false;
        }

        public GroupField(int parentGroupId, string text, bool income)
        {
            dRow = KMData.DataTableGroups.NewRow();
            dRow["ParentGroupId"] = parentGroupId;
            dRow["Name"] = text;
            dRow["IorE"] = income;
            add = true;
        }

        int index = 0;//由于没有做排序,导致Combox中索引值无法对应Id值,所以做此属性
        public int Index
        {
            get { return index; }
        }

        public string Note
        {
            get { return (string)dRow["Note"]; }
            set { dRow["Note"] = value; }
        }

        public int Id
        {
            get { return (int)dRow["Id"]; }
        }

        public int ParentGroupId
        {
            get { return (int)dRow["ParentGroupId"]; }
            set { dRow["ParentGroupId"] = value; }
        }

        public string Name
        {
            get { return (string)dRow["Name"]; }
            set { dRow["Name"] = value; }
        }

        public bool IorE
        {
            get { return (bool)dRow["IorE"]; }
            set { dRow["IorE"] = value; }
        }

        public int IconIndex
        {
            get { return (int)dRow["IconIndex"]; }
            set { dRow["IconIndex"] = value; }
        }
    }
}